﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using FrontDisplay.Weather;

namespace FrontDisplay.Clock
{
    public class ClockPlugin : PluginBase
    {
        private readonly DispatcherTimer _timer;
        private bool _isColon;
        private DateTime _currentTime;
        private readonly ClockControl _clockControl;
        private readonly DateControl _dateControl;
        private WeatherPlugin _weather;

        public ClockPlugin()
        {
            IsBlink = true;

            _clockControl = new ClockControl();
            _dateControl = new DateControl { HorizontalAlignment = HorizontalAlignment.Right };
            UpdateDate();

            _timer = new DispatcherTimer();
            _timer.Tick += delegate { UpdateTime(); };
            _timer.Interval = TimeSpan.FromMilliseconds(500);
            _timer.Start();
        }

        public bool IsBlink { get; set; }

        private void UpdateTime()
        {
            _isColon = !_isColon;
            if (IsBlink)
            {
                _clockControl.ColonLabel.Visibility = _isColon ? Visibility.Visible : Visibility.Hidden;
            }
            else
            {
                _clockControl.ColonLabel.Visibility = Visibility.Visible;
            }

            if (_currentTime.Minute != DateTime.Now.Minute)
            {
                _clockControl.MinuteLabel.Text = DateTime.Now.ToString("mm");
                if (_currentTime.Hour != DateTime.Now.Hour)
                {
                    _clockControl.HourLabel.Text = DateTime.Now.ToString("HH");
                    if (_currentTime.Day != DateTime.Now.Day)
                    {
                        UpdateDate();
                    }
                }
                _currentTime = DateTime.Now;
                InfoPage.Instance.UpdateTime();
            }
        }

        private void UpdateDate()
        {
            _dateControl.DateLabel.Text = DateTime.Now.Day.ToString();
            _dateControl.MonthLabel.Text = DateTime.Now.ToString("d MMMM", Thread.CurrentThread.CurrentUICulture.DateTimeFormat).Remove(0, 2).Trim().ToLower();
            if (_weather != null) _weather.UpdateDate();
        }

        public override string Title
        {
            get { return Def.Localization.GetString("Global.Clock"); }
        }

        public override void Start()
        {
            InfoPage.Instance.ShowCenterInfo(_clockControl);
            _weather = MainWindow.GetPlugin<WeatherPlugin>();
            //IsActive = true;
        }

        public override void Stop() { }

        public override Control PageControl { get { return null; } }

        public override UIElement TopControl { get { return null; } }

        public override UIElement BottomControl
        {
            get { return _dateControl; }
        }

        public override UIElement ParametersControl
        {
            get { return null; }
        }
    }
}
